<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>



      <el-form-item label="群体" prop="ifGroup">
        <el-select v-model="queryParams.ifGroup" placeholder="请选择营销任务类型" clearable size="small">
          <el-option v-for="dict in ifGroupOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['buss:sqzxt:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['buss:taskInfo:export']">数据导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="" size="mini" @click="importTaskInfo" v-hasPermi="['buss:taskInfo:allotTaskCustomer']">上传文件导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="" size="mini" @click="importxls" v-hasPermi="['buss:taskInfo:allotTaskCustomer']">模板导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="taskId" :show-overflow-tooltip="true" />
      <el-table-column label="任务名称" align="center" prop="taskName" :show-overflow-tooltip="true" />
      <el-table-column label="目标客户类型" align="center" prop="ifGroup" :formatter="ifGroupFormat" />
      <el-table-column label="客户经理" align="center" prop="userId" :formatter="userIdFormat" />
      <el-table-column label="机构ID" align="center" prop="orgId" :formatter="orgIdFormat" />
      <el-table-column label="任务开始时间" align="center" prop="beginDate" />
      <el-table-column label="任务结束时间" align="center" prop="endDate" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link icon="el-icon-s-order" :to="{ path: '/sq/taskQ/', query: { sqBG: scope.row } }" class="link-type"><span>详情  </span></router-link>
          <!-- <el-button size="mini" type="text" icon="el-icon-s-order" @click="handleUpdate(scope.row)" v-hasPermi="['buss:taskInfo:edit']">详情</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['buss:taskInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改营销任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="addTask" :model="addTask" :rules="rules" label-width="120px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="addTask.taskName" placeholder="请输入任务名称" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>

        <el-form-item label="任务来源" prop="rwly">
          <el-radio-group v-model="rwly" @change="radiorwly(rwly)" size="small">
            <el-radio v-for="(item, index) in taskSyTypeList" :key="index" :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="首页任务" prop="sybd" v-show="isShowT">
          <el-select v-model="sybdvalue" clearable placeholder="请选择"><el-option v-for="item in sybdList" :key="item.value" :label="item.label" :value="item.value"></el-option></el-select>
        </el-form-item>

        <el-form-item label="营销目标" prop="taskTarget" v-show="isShow">
          <el-select v-model="taskTargetvalue" clearable placeholder="请选择"><el-option v-for="item in taskTargetList" :key="item.value" :label="item.label" :value="item.value"></el-option></el-select>
        </el-form-item>

        <el-form-item label="营销方案" prop="planName" v-show="isShow">
          <el-select v-model="planNamevalue" clearable placeholder="请选择"><el-option v-for="item in planNameList" :key="item.planCode" :label="item.planName" :value="item.planCode"></el-option></el-select>
        </el-form-item>

        <el-form-item label="任务开始时间" prop="beginDate">
          <el-date-picker
              @change="change"
                v-model="beginDate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
        </el-form-item>

        <el-form-item label="任务结束时间" prop="endDate">
          <el-date-picker
              @change="change"
                v-model="endDate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="addTask.remark" placeholder="请输入备注" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改营销任务对话框 -->
    <el-dialog :title="title" :visible.sync="importTaskInfoOpen" width="500px" append-to-body>
      <el-form ref="addTask" :model="addTask" :rules="rules" label-width="100px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="addTask.taskName" placeholder="请输入任务名称" clearable :style="{ width: '100%' }"></el-input>
        </el-form-item>
        <el-form-item label="任务开始时间" prop="beginDate">
          <el-date-picker
              @change="change"
                v-model="beginDate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
        </el-form-item>

        <el-form-item label="任务结束时间" prop="endDate">
          <el-date-picker
              @change="change"
                v-model="endDate"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
        </el-form-item>
        <el-form-item label="上传文件" prop="taskName">
          <el-upload
            class="upload-demo"
            action=""
            :http-request="upLoadOnSelf">
            <el-button type="primary" plain icon="" size="mini" v-hasPermi="['buss:taskInfo:allotTaskCustomer']">上传文件导入</el-button>
          </el-upload>

        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormTaskInfo">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTaskInfo,
    getTaskInfo,
    delTaskInfo,
    addTaskInfo,
    updateTaskInfo,
    queryPlanNew,
    insert,
    orglist,
    queryManager,
    taskListCustom,
    importTaskInfoExces
  } from "@/api/buss/taskInfo";

  export default {
    name: "TaskInfo",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        rwly: '',
        isShow: false,
        isShowT: false,
        // 总条数
        total: 0,
        beginDate: '',
        endDate: '',
        // 营销任务表格数据
        taskInfoList: [],
        planNameList: [],
        managervalue: '',
        orgvalue: '',
        orgList: [],
        manger: {
          orgId: ''
        },
        mangerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openUpdata: false,
        importTaskInfoOpen: false,
        planNamevalue: '',
        addTask: {
          taskName: '',
          beginDate: '',
          endDate: '',
          ifGroup: '',
          taskType: '',
          remark: '',
          groupCode: '',
          taskTarget: '',
          taskSyType: ''
        },
        taskTargetList: [
          {
            value: 'CK',
            label: '存款'
          },
          {
            value: 'DK',
            label: '贷款'
          },
          {
            value: 'ETC',
            label: 'ETC'
          },
          {
            value: 'XYK',
            label: '信用卡'
          },
          {
            value: 'SJYH',
            label: '手机银行'
          },
          {
            value: 'WY',
            label: '网银'
          },
          {
            value: 'DXQY',
            label: '短信签约'
          },
          {
            value: 'SDDJ',
            label: '水电代缴'
          },
          {
            value: 'SYB',
            label: '收银宝'
          },
          {
            value: 'POS',
            label: 'POS'
          }
        ],
        taskTargetvalue: '',
        sybdList: [
          {
            value: '1',
            label: '大额资金变动'
          },
          {
            value: '2',
            label: '存款到期'
          },
          {
            value: '3',
            label: '理财到期'
          },
          {
            value: '4',
            label: '贷款到期'
          },
          {
            value: '5',
            label: '生日提醒'
          }
        ],
        sybdvalue: '',
        taskSyTypeList: [
          {
            value: '0',
            label: '首页任务'
          },
          {
            value: '1',
            label: '营销任务'
          }
        ],
        taskSyTypevalue: '',
        // ID字典
        taskIdOptions: [],
        // 任务名称字典
        taskNameOptions: [],
        // 营销任务类型(0总行，1个人)字典
        taskTypeOptions: [],
        // 目标客户类型字典
        ifGroupOptions: [],
        // 任务开始时间字典
        beginDateOptions: [],
        // 任务结束时间字典
        endDateOptions: [],
        // 已营销客户数字典
        finishNumOptions: [],
        // 客户经理字典
        userIdOptions: [],
        // 添加时间字典
        createTimeOptions: [],
        // 修改时间字典
        updateTimeOptions: [],
        // 添加人字典
        createUserOptions: [],
        // 操作员字典
        updateUserOptions: [],
        // 状态  0不正常  1正常字典
        statusOptions: [],
        // 备注字典
        remarkOptions: [],
        // 营销目标字典
        taskTargetOptions: [],
        // 机构ID字典
        orgIdOptions: [],
        // 营销状态  0未完成  1已完成字典
        completeOptions: [],
        // 任务来源0普通1是首页(首页列如生日提醒,理财到期)字典
        taskSytypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          taskName: null,
          taskType: 0,
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: null,
          remark: null,
          taskTarget: null,
          orgId: null,
          complete: null,
          taskSytype: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        taskListCustomList: [],
        totalxq: 0,
        taskIdS: '',
        newFile: new FormData(),
        taskInfoFile: ''
      };
    },
    created() {
      this.getList();

      this.getDicts("ifGroup").then(response => {
        this.ifGroupOptions = response.data;
      });

      this.getDicts("sys_user_id").then(response => {
        this.userIdOptions = response.data;
      });

      this.getDicts("sys_org_id").then(response => {
        this.orgIdOptions = response.data;
      });
    },
    methods: {
      /** 查询营销任务列表 */
      getList() {
        this.loading = true;
        listTaskInfo(this.queryParams).then(response => {
          this.taskInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        //下拉机构
        orglist().then(response => {
          this.orgList = response.rows;
        });
        queryPlanNew().then(response => {
          this.planNameList = response.rows;
          //console.log(JSON.stringify(this.sssjsqList))
        });
      },
      submitFormTaskInfo() {
        let fromData = this.newFile;
        fromData.append('file',this.taskInfoFile)
        fromData.append('taskName',this.addTask.taskName)
        fromData.append('endDate',this.addTask.endDate)
        fromData.append('beginDate',this.addTask.beginDate)

        importTaskInfoExces(fromData).then(response => {
          this.msgSuccess("上传成功");
          this.importTaskInfoOpen = false;
          this.getList();
        });
      },
      upLoadOnSelf(fileParam) {
        this.taskInfoFile = fileParam.file;
      },
      //客户经理
      orgIdBlur(mg) {
        this.manger.orgId = mg;
        queryManager(this.manger).then(response => {
          this.mangerList = response.rows;
        });
      },
      radiorwly(rwly) {
        //console.log(rwly)
        if(rwly == '1'){
          this.isShow = true;
          this.isShowT = false;
        }else{
          this.isShow = false;
          this.isShowT = true;
        }
      },
      change() {
        var bgd = this.beginDate.split("-");
        var bj = bgd.join("")
        var edb = this.endDate.split("-");
        var ej = edb.join("")
        this.addTask.beginDate = this.beginDate;
        this.addTask.endDate = this.endDate;
          console.log(bj);
        },
      // ID字典翻译
      taskIdFormat(row, column) {
        return this.selectDictLabel(this.taskIdOptions, row.taskId);
      },
      // 任务名称字典翻译
      taskNameFormat(row, column) {
        return this.selectDictLabel(this.taskNameOptions, row.taskName);
      },
      // 营销任务类型(0总行，1个人)字典翻译
      taskTypeFormat(row, column) {
        return this.selectDictLabel(this.taskTypeOptions, row.taskType);
      },
      // 目标客户类型字典翻译
      ifGroupFormat(row, column) {
        return this.selectDictLabel(this.ifGroupOptions, row.ifGroup);
      },
      // 任务开始时间字典翻译
      beginDateFormat(row, column) {
        return this.selectDictLabel(this.beginDateOptions, row.beginDate);
      },
      // 任务结束时间字典翻译
      endDateFormat(row, column) {
        return this.selectDictLabel(this.endDateOptions, row.endDate);
      },
      // 已营销客户数字典翻译
      finishNumFormat(row, column) {
        return this.selectDictLabel(this.finishNumOptions, row.finishNum);
      },
      // 客户经理字典翻译
      userIdFormat(row, column) {
        return this.selectDictLabel(this.userIdOptions, row.userId);
      },
      // 添加时间字典翻译
      createTimeFormat(row, column) {
        return this.selectDictLabel(this.createTimeOptions, row.createTime);
      },
      // 修改时间字典翻译
      updateTimeFormat(row, column) {
        return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
      },
      // 添加人字典翻译
      createUserFormat(row, column) {
        return this.selectDictLabel(this.createUserOptions, row.createUser);
      },
      // 操作员字典翻译
      updateUserFormat(row, column) {
        return this.selectDictLabel(this.updateUserOptions, row.updateUser);
      },
      // 状态  0不正常  1正常字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 备注字典翻译
      remarkFormat(row, column) {
        return this.selectDictLabel(this.remarkOptions, row.remark);
      },
      // 营销目标字典翻译
      taskTargetFormat(row, column) {
        return this.selectDictLabel(this.taskTargetOptions, row.taskTarget);
      },
      // 机构ID字典翻译
      orgIdFormat(row, column) {
        return this.selectDictLabel(this.orgIdOptions, row.orgId);
      },
      // 营销状态  0未完成  1已完成字典翻译
      completeFormat(row, column) {
        return this.selectDictLabel(this.completeOptions, row.complete);
      },
      // 任务来源0普通1是首页(首页列如生日提醒,理财到期)字典翻译
      taskSytypeFormat(row, column) {
        return this.selectDictLabel(this.taskSytypeOptions, row.taskSytype);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.importTaskInfoOpen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          taskId: null,
          taskName: null,
          taskType: null,
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: "0",
          remark: null,
          taskTarget: null,
          orgId: null,
          complete: null,
          taskSytype: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      importTaskInfo() {
        this.form = {
          taskName: null,
          beginDate: null,
          endDate: null
        };
        this.importTaskInfoOpen = true;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.taskId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加营销任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const taskId = row.taskId || this.ids
        this.taskIdS = taskId;
        this.openUpdata = true;
        this.title = "修改营销任务";
        var yxData = {
          taskId: this.taskIdS
        }

        taskListCustom(yxData).then(response => {
          this.taskListCustomList = response.rows;
          this.totalxq = response.total;

        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["addTask"].validate(valid => {
          if (valid) {
            if (this.form.taskId != null) {
              updateTaskInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              var addTaskInfo = {
                taskName: this.addTask.taskName,
                beginDateStr: this.addTask.beginDate,
                endDateStr: this.addTask.endDate,
                ifGroup: '0',
                taskType: '0',
                remark: this.addTask.remark,
                groupCode: this.planNamevalue,
                userId: '',
                orgId: '',
                taskTarget: this.taskTargetvalue,
                taskSyType: this.sybdvalue,
              }

              insert(addTaskInfo).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const taskIds = row.taskId || this.ids;
        this.$confirm('是否确认删除营销任务编号为"' + taskIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTaskInfo({taskId:taskIds});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      importxls() {
        window.open('http://32.14.33.6:5004/buss/file/file?filePath=RWMB.xlsx')

      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('buss/taskInfo/exportZh', {
          ...this.queryParams
        }, `总行任务导出.xlsx`)
      }
    }
  };
</script>
